﻿using B.Z.BasicData.Infrastructure;
using B.Z.Inventory.API.Read.Application.Command;
using B.Z.Inventory.API.Read.DTO;
using B.Z.Inventory.Domain;
using B.Z.Inventory.ErrorCode;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.Z.Inventory.API.Read.Application.CommandHandler
{
    public class IntOutStoreQueryCommandHandler : IRequestHandler<IntOutStoreQueryCommand, APIResult<APIPaging<IntOutStoreDto>>>
    {
        private readonly IBaseRepository<T_IntOutStore> intoutstoreRepository;
        private readonly IBaseRepository<T_Goods> goodsRepository;
        private readonly IBaseRepository<T_Store> storeRepository;
        private readonly ILogger<IntOutStoreQueryCommandHandler> logger;
        public IntOutStoreQueryCommandHandler(IBaseRepository<T_IntOutStore> intoutstoreRepository, IBaseRepository<T_Store> storeRepository, ILogger<IntOutStoreQueryCommandHandler> logger, IBaseRepository<T_Goods> goodsRepository)
        {
            this.intoutstoreRepository = intoutstoreRepository;
            this.storeRepository = storeRepository;
            this.logger = logger;
            this.goodsRepository = goodsRepository;
        }

        public Task<APIResult<APIPaging<IntOutStoreDto>>> Handle(IntOutStoreQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<IntOutStoreDto>> result = new APIResult<APIPaging<IntOutStoreDto>>();
            result.Code = APIEnums.ok;
            result.Message = "获取成功";

            var list = (from a in intoutstoreRepository.GetAll().AsNoTracking().Where(x => x.IsIntOrOut == request.IsIntOrOut)
                       join b in storeRepository.GetAll().AsNoTracking()
                       on a.StoreId equals b.Id
                       select new IntOutStoreDto
                       {
                           Id = a.Id,
                           InventoryId = a.InventoryId,
                           InStoreNo = a.InStoreNo,
                           GoodsIds = a.GoodsIds,
                           IntOutStoreTime = a.IntOutStoreTime,
                           StoreId = a.StoreId,
                           Count = a.Count,
                           Reson = a.Reson,
                           IsIntOrOut = a.IsIntOrOut,
                           AttnId = a.AttnId,
                           AttnName = a.AttnName,
                           CreateUserName = a.CreateUserName,
                           StoreName = b.StoreName,
                           CreateTime = a.CreateTime,
                           GoodsName = "",
                       }).ToList();
            APIPaging<IntOutStoreDto> paging = new APIPaging<IntOutStoreDto>();
            paging.Count = list.Count();
            foreach (var item in list)
            {
                var ids= item.GoodsIds.Split(',').Select(x => Convert.ToInt32(x));
                var models = goodsRepository.GetAll().Where(x => ids.Contains(x.Id)).Select(x => x.GoodsName);
                item.GoodsName = string.Join("、", models);
            }
            list = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
          
            paging.List = list;
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
